home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / MacWT 0.04 / wt / wt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  4.1 KB  |  151 lines  |  [TEXT/MMCC]

  1. /*
  2. **  wt -- a 3d game engine
  3. **
  4. **  Copyright (C) 1994 by Chris Laurel
  5. **  email:  claurel@mr.net
  6. **  snail mail:  Chris Laurel, 5700 W Lake St #208,  St. Louis Park, MN  55416
  7. **
  8. **  This program is free software; you can redistribute it and/or modify
  9. **  it under the terms of the GNU General Public License as published by
  10. **  the Free Software Foundation; either version 2 of the License, or
  11. **  (at your option) any later version.
  12. **
  13. **  This program is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. **  GNU General Public License for more details.
  17. **
  18. **  You should have received a copy of the GNU General Public License
  19. **  along with this program; if not, write to the Free Software
  20. **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <math.h>
  26. #include "wt.h"
  27. #include "error.h"
  28. #include "fixed.h"
  29. #include "view.h"
  30. #include "texture.h"
  31. #include "table.h"
  32. #include "world.h"
  33. #include "worldfile.h"
  34. #include "framebuf.h"
  35. #include "render.h"
  36. #include "graphics.h"
  37. #include "input.h"
  38.  
  39. View *view;
  40.  
  41.  
  42. int main(int argc, char *argv[])
  43. {
  44.      World *w;
  45.      FILE *fp;
  46.      Boolean quit = False;
  47.      Intent *intent;
  48.      fixed v = FIXED_ZERO;
  49.      double vx = 0.0, vy = 0.0, va = 0.0;
  50.  
  51.      
  52.      if (argc != 2) {
  53.       fprintf(stderr, "Usage:  wt <world file>\n");
  54.       exit(EXIT_FAILURE);
  55.      }
  56.  
  57.      if ((fp = fopen(argv[1], "r")) == NULL) {
  58.       perror(argv[1]);
  59.       exit(EXIT_FAILURE);
  60.      }
  61.      w = read_world_file(fp);
  62.      fclose(fp);
  63.  
  64.      init_graphics();
  65.      init_renderer(SCREEN_WIDTH, SCREEN_HEIGHT);
  66.      init_input_devices();
  67.  
  68.      /* setup view */
  69.      view = new_view(fixdiv(FIXED_2PI, INT_TO_FIXED(4)));
  70.  
  71.      view->x = FIXED_ZERO;
  72.      view->y = FIXED_ZERO;
  73.      view->height = FIXED_ONE;
  74.      view->angle = FIXED_ZERO;
  75.  
  76.      while (!quit) {
  77.       double sin_facing, cos_facing;
  78.  
  79.       render(w, view);
  80.       intent = read_input_devices();
  81.  
  82.       /* This block code is a hack to do acceleration and deceleration. */
  83.       if (fabs(vx) > fabs(intent->force_x)) {
  84.            if (vx < 0.0)
  85.             vx = MIN(vx + 0.1, intent->force_x);
  86.            else
  87.             vx = MAX(vx - 0.1, intent->force_x);
  88.       } else if (fabs(vx) < fabs(intent->force_x)) {
  89.            vx += intent->force_x / 5.0;
  90.            if (fabs(vx) > fabs(intent->force_x))
  91.             vx = intent->force_x;
  92.       }
  93.       if (fabs(vy) > fabs(intent->force_y)) {
  94.            if (vy < 0.0)
  95.             vy = MIN(vy + 0.1, intent->force_y);
  96.            else
  97.             vy = MAX(vy - 0.1, intent->force_y);
  98.       } else if (fabs(vy) < fabs(intent->force_y)) {
  99.            vy += intent->force_y / 5.0;
  100.            if (fabs(vy) > fabs(intent->force_y))
  101.             vy = intent->force_y;
  102.       }
  103.       if (fabs(vy) > fabs(intent->force_y)) {
  104.            if (vy < 0.0)
  105.             vy = MIN(vy + 0.1, intent->force_y);
  106.            else
  107.             vy = MAX(vy - 0.1, intent->force_y);
  108.       } else if (fabs(vy) < fabs(intent->force_y)) {
  109.            vy += intent->force_y / 5.0;
  110.            if (fabs(vy) > fabs(intent->force_y))
  111.             vy = intent->force_y;
  112.       }
  113.       /* Angular deceleration here is weird and unrealistic, but it feels
  114.       **   right to me.
  115.       */
  116.       if (fabs(va) > fabs(intent->force_rotate))
  117.            va *= 0.6;
  118.       else if (fabs(va) < fabs(intent->force_rotate)) {
  119.            va += intent->force_rotate / 8.0;
  120.            if (fabs(va) > fabs(intent->force_rotate))
  121.             va = intent->force_rotate;
  122.       }
  123.       view->angle += FLOAT_TO_FIXED(0.3 * va);
  124.       sin_facing = sin(FIXED_TO_FLOAT(view->angle));
  125.       cos_facing = cos(FIXED_TO_FLOAT(view->angle));
  126.            
  127.       view->x += FLOAT_TO_FIXED(0.8 * vx * cos_facing);
  128.       view->y += FLOAT_TO_FIXED(0.8 * vx * sin_facing);
  129.       view->x += FLOAT_TO_FIXED(0.8 * vy * -sin_facing);
  130.       view->y += FLOAT_TO_FIXED(0.8 * vy * cos_facing);
  131.       if (view->height > FIXED_ONE)
  132.            v -= FIXED_ONE / 16;
  133.       view->height += v;
  134.       if (view->height < FIXED_ONE) {
  135.            v = FIXED_ZERO;
  136.            view->height = FIXED_ONE;
  137.       }
  138.       while (intent->n_special--) {
  139.            if (intent->special[intent->n_special] == INTENT_END_GAME)
  140.             quit = True;
  141.            else
  142.             v = FIXED_ONE / 2;
  143.       }
  144.      }
  145.  
  146.      end_input_devices();
  147.      end_graphics();
  148.  
  149.      return EXIT_SUCCESS;
  150. }
  151.